﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.ComponentModel;
using XMLSpreadGear.Templates;

namespace XMLSpreadGear.Core
{
    public abstract class BaseElement
    {
        private string m_strlocalname;
               
        public BaseElement(string strLocalName)
        {
            this.m_strlocalname = strLocalName;
            SetDefaults();
        }

        protected virtual string LocalName
        { get { return m_strlocalname; } }

        internal protected abstract IDictionary<string, string> RegisterElements();

        internal protected abstract IDictionary<string, string> RegisterAttributes();

        protected abstract void SetDefaults();

        protected XmlAttribute GenerateAttribute(XmlDocument xdock, string attrName)
        {
            XmlAttribute attr;

            if (string.IsNullOrEmpty(GetAttributeNamespace()))
                attr = xdock.CreateAttribute(attrName);
            else
                attr = xdock.CreateAttribute(GetAttributeNamespace(), attrName, GetAttributeNamespaceURI());

            return attr;
        }

        public virtual XmlDocument GetElement()
        {
            XmlDocument xdock = new XmlDocument();
            XmlNode thisnode = xdock.CreateNode(XmlNodeType.Element, this.LocalName, string.Empty);

            IDictionary<string, string> elements = this.RegisterElements();
            IDictionary<string, string> attributes = this.RegisterAttributes();

            if (attributes.Count > 0)
            {
                for (int i = 0; i < attributes.Count; i++)
                {
                    
                    XmlAttribute attr = GenerateAttribute(xdock,attributes.Keys.ElementAt(i));

                    //if (string.IsNullOrEmpty(GetAttributeNamespace()))
                    //    attr = xdock.CreateAttribute(attributes.Keys.ElementAt(i));
                    //else
                    //    attr = xdock.CreateAttribute(GetAttributeNamespace(), attributes.Keys.ElementAt(i), string.Empty);

                    attr.Value = attributes[attributes.Keys.ElementAt(i)];
                    
                    thisnode.Attributes.Append(attr);
                }
            }

            if (elements.Count > 0)
            {
                for (int i = 0; i < elements.Count; i++)
                {
                    //XmlNode element = xdock.CreateElement(elements.Keys.ElementAt(i));
                    //XmlNode element = xdock.CreateNode(XmlNodeType.Element, elements.Keys.ElementAt(i), string.Empty);
                    //element.InnerText = elements[element.Name];
                    //thisnode.AppendChild(element);
                    thisnode.InnerXml = thisnode.InnerXml + elements[elements.Keys.ElementAt(i)];
                }
            }

            xdock.AppendChild(thisnode);
            return xdock;
        }

        public override string ToString()
        {
            return GetElement().InnerXml;
        }

        internal string AddGenericElement(string elementName, string elementValue)
        {
            GenericElement eg = new GenericElement(elementName);
            IDictionary<string, string> dicelement = new Dictionary<string, string>();
            dicelement.Add(elementName, elementValue);
            eg.Elements = dicelement;

            return eg.ToString();
        }

        internal virtual string GetAttributeNamespace()
        {
            return string.Empty;
        }

        internal virtual string GetAttributeNamespaceURI()
        {
            return string.Empty;
        }
                
    }
}
